package com.ant.storage.controller;


import com.alibaba.excel.EasyExcel;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.HouseLeaseDto;
import com.ant.storage.excel.HouseLeaseExcel;
import com.ant.storage.pojo.HouseAccount;
import com.ant.storage.pojo.HouseLease;
import com.ant.storage.service.ICapitalAccountService;
import com.ant.storage.service.IHouseAccountService;
import com.ant.storage.service.IHouseLeaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2023-11-13
 */
@RestController
@RequestMapping("/houseLease")
public class HouseLeaseController {

    @Autowired
    private IHouseLeaseService leaseService;

    @Autowired
    private IHouseAccountService accountService;

    @Autowired
    private ICapitalAccountService capitalAccountService;

    @ApiOperation(value = "获取物品库存数据列表")
    @GetMapping("/getHouseLeaseList")
    public ResultPage getHouseLeaseList(HouseLeaseDto dto) {

       // return  leaseService.queryList(dto)
        return ResultPage.success("操作成功", leaseService.queryList(dto));
    }
    //TODO 修改日期 同步更改状态

    @ApiOperation(value = "id查询详情")
    @GetMapping("/findHouseLeaseById/{id}")
    public ResultVo findHouseLeaseById(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功", leaseService.findHouseLeaseById(id));
    }

    @ApiOperation(value = "保存信息")
    @PostMapping("/houseLeaseSave")
    public ResultVo houseLeaseSave(@RequestBody HouseLease dto) {
        return leaseService.houseLeaseSave(dto);
    }

    @ApiOperation(value = "保存历史合同信息")
    @PostMapping("/houseLeaseSaves")
    public ResultVo houseLeaseSaves(@RequestBody HouseLease dto) {
        return leaseService.houseLeaseSaves(dto);
    }


    @ApiOperation(value = "修改租赁信息")
    @PostMapping("/houseLeaseUpdate")
    public ResultVo houseLeaseUpdate(@RequestBody HouseLease dto) {
        return leaseService.houseLeaseUpdate(dto);
    }


    @ApiOperation(value = "查询账号信息")
    @GetMapping("/findHouseAccountParentId/{id}")
    public ResultVo findHouseAccountParentId(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功",
                accountService.list(new QueryWrapper<HouseAccount>().eq("parent_id",id)));
    }

    @ApiOperation(value = "获取报销类型")
    @GetMapping("/findCostType")
    public ResultVo findCostType() {
        return ResultVo.success("操作成功",  capitalAccountService.costTypeList());
    }

    @ApiOperation(value = "获取合同名列表")
    @GetMapping("/houseLeaseNameList/{id}")
    public ResultVo houseLeaseNameList(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功",  leaseService.houseLease(id));
    }

    //导出
    @ApiOperation(value = "导出")
    @RequestMapping(value = "/downLoadExportCensus", method = RequestMethod.GET)
    public void downLoadExportCensus(@RequestParam(value = "isRecycle") Integer isRecycle,
                                     HttpServletResponse response
    ) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("合同信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<HouseLeaseExcel> orderList = leaseService.exportCensus(isRecycle);
        EasyExcel.write(response.getOutputStream(), HouseLeaseExcel.class)
                .sheet("合同信息").doWrite(orderList);
    }

}
